-- Based on haste's oCD (http://ixo.no/git/)

local defaults = {
	size = 30,
	min = 2.0,
	max = 20*60,
	textPos = "right",
	growth = "down",
}

local spells = {
	["35476"] = 120, -- Drums of Battle
	["2825"] = 600,  -- Bloodlust
	["29166"] = 360, -- Innervate
	["16190"] = 300, -- Mana Tide
	["26994"] = 1200,-- Rebirth
	["6346"] = 180   -- Fear Ward

	--["34433"] = 300  -- Shadowfiend (for testing)
}

local addon = CreateFrame"Frame"
addon:Hide()

local print = function(...) ChatFrame1:AddMessage(...) end
local printf = function(...) ChatFrame1:AddMessage(string.format(...)) end

function addon:UnitGUIDInRaid(guid)
	for i = 0, GetNumRaidMembers() do
		local pguid = UnitGUID("raid"..i)
		if pguid == guid then
			return true
		end
	end
	return false
end

-- TODO: Remove the global reference to this table.
local timers = {}
local rcd = {}
addon.rcd = rcd
addon.timers = timers
RaidCDDB = {}

local time, duration, enable
local updateCooldown = function(self)
	for name, obj in pairs(addon.timers) do
		local p = obj.player
		local id = tostring(obj.spellid)
		if rcd[p.."_"..id] and GetTime() < rcd[p.."_"..id]+spells[id] then
			time = rcd[p.."_"..id]
			duration = spells[id]
		else 
			time = 0
			duration = 0
			rcd[p.."_"..id] = nil
		end
		obj(duration, time)
	end
end

--[[ Initiate the addon
--]]
local register, setMinMax, setTextPosition
function addon:PLAYER_LOGIN()
	register = self.bars.register
	setMinMax = self.bars.setMinMax
	setTextPosition = self.bars.setTextPosition
	self.db = RaidCDDB

	if not self.db.size then self.db.size = defaults.size end
	setMinMax(defaults.min, defaults.max)
	self.bars.CreateMainFrame()
	self:updatePositions()
end


local check = function(self, event, time, cevent, sGUID, sName, sFlags, dGUID, dName, dFlags, spellid, spellname)
	local id = tostring(spellid)
	if raidcd:UnitGUIDInRaid(sGUID) then
		if id and spells[id] then
			updateCooldown()
			if cevent == "SPELL_CAST_SUCCESS" then
				register(spellname, spellid, sName)
				rcd[sName.."_"..id] = GetTime()
				setMinMax(defaults.min, defaults.max)
				setTextPosition(defaults.textPos)
				updateCooldown()
			end
		end
	end
end

addon.PLAYER_ENTERING_WORLD = updateCooldown

--[[ We delay these events as they can trigger extra scans.
--]]
local show = function(...) check(...) end
addon.COMBAT_LOG_EVENT = show
addon.COMBAT_LOG_EVENT_UNFILTERED = show

function addon:Test()
	register("Drums of Battle", "35476", "TestPlayer")
	register("Bloodlust", "2825", "TestPlayer")
	rcd["TestPlayer_35476"] = GetTime()
	rcd["TestPlayer_2825"] = GetTime()
	setTextPosition(defaults.textPos)
	updateCooldown()
end

function addon:updatePositions(n)
	if(self.db.Pos) then
		local x, y = strsplit(" ", self.db.Pos)
		local s = self.frame:GetEffectiveScale()
		
		self.frame:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x/s, y/s)
	elseif(self.frame) then
		self.frame:ClearAllPoints()
		self.frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
	end
end

function addon:savePosition()
	local f = self.frame
	local x,y = f:GetLeft(), f:GetTop()
	local s = f:GetEffectiveScale()
	
	x,y = x*s,y*s
	
	self.db.Pos = strjoin(" ", x, y)
end

--[[ We delay all updates with .5 sec
--]]

addon:SetScript("OnEvent", function(self, event, ...)
	self[event](self, event, ...)
end)

addon:RegisterEvent"PLAYER_LOGIN"
addon:RegisterEvent"PLAYER_ENTERING_WORLD"

addon:RegisterEvent"COMBAT_LOG_EVENT"
addon:RegisterEvent"COMBAT_LOG_EVENT_UNFILTERED"

SlashCmdList["RAIDCD"] = function(cmd)
	local a1, a2 = strsplit(" ", cmd)
	if cmd == "show" then
		raidcd.frame:Show()
	elseif cmd == "hide" then
		raidcd.frame:Hide()
	elseif cmd == "test" then
		raidcd:Test()
	elseif a1 == "size" then
		raidcd.db.size = a2
	else
		print"/raidcd show - Shows the movable frame."
		print"/raidcd hide - Hide the movable frame."
		print"/raidcd test - Shows testbars."
		print"/raidcd size - changes the size of the icon/text [Default is 30]"
	end
end

SLASH_RAIDCD1 = "/raidcd"

_G.raidcd = addon

